<template>
	<view>
		<view class="cu-timeline">
			<block v-for="(val, key) in messages" :key="key">
				<view class="cu-time">{{ key.substring(5) }}</view>
				<view class="cu-item text-blue">
					<view class="bg-blue shadow-blur content">
						<view class="cu-list menu-avatar radius">
							<view class="cu-item" v-for="(item, i) in val" :key="item.id">
								<view class="cu-avatar round lg" :style="'background-image:url(' + item.url + '),url(/static/img/none.jpg);'"></view>
								<view class="content">
									<view class="text-grey">{{ item.extras.userNikename }}</view>
									<view class="text-gray text-sm">
										<text class="cuIcon-infofill"></text>
										{{ item.content }}
									</view>
								</view>
								<view class="action">
									<view v-show="item.isRead == 1" class="cu-tag round bg-grey">已同意</view>
									<view v-show="item.isRead == 0" class="cu-tag round bg-green" @click.stop="addFriend(item)">添加</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			messages: {}
		};
	},
	onShow() {
		this.onRefresh();
	},
	methods: {
		onRefresh() {
			console.log('onRefresh');
			// #ifndef  H5
			let user=this.$store.state.userInfo;
			let sql = 'SELECT * FROM "message" WHERE "from"=\'{0}\' AND "to"=\'{1}\' ORDER BY createTime DESC LIMIT 100'.format(0, user.userId);
			this.db.selectSQL(
				sql,
				function(datas) {
					let json = {};
					for (let data of datas) {
						data.extras = JSON.parse(data.extras);
						data.url = [this.getImgUrl('user',data.from)];
						let key = this.moment(parseInt(data.createTime)).format('YYYY-MM-DD');
						if (this.isNotBlank(json[key])) {
							json[key].push(data);
						} else {
							json[key] = [];
							json[key].push(data);
						}
					}
					this.messages = json;
					console.log(JSON.stringify(this.messages), 'messages');
				}.bind(this)
			);
			// #endif
			// #ifdef  H5
			this.messages = {
				'2020-01-14': [
					{
						id: '7669710a100942ca85f35e740f538af0',
						chatType: 0,
						cmd: 11,
						msgType: 0,
						content: '添加好友',
						createTime: '1578992850982',
						from: '0',
						to: '32',
						group_id: '',
						extras: { userNikename: 'test2', from: '31', title: '验证消息',userMobile: '13852657213' },
						isRead: 1,
						url: ['http://img1.3lian.com/gif/more/11/201212/0d1252b54be4f2d240b6b7fe4ed35054.jpg']
					}
				]
			};
			// #endif
		},
		async addFriend(item){
			console.log(item);
			let user=this.$store.state.userInfo;
			let userFriend={};
			userFriend.userId=item.extras.from;
			userFriend.friendId=item.to;
			userFriend.friendNickName=item.extras.userNikename;
			userFriend.friendMobile=user.userMobile;
			let my={};
			my.userId=user.userId;
			my.friendId=item.extras.from;
			my.friendNickName=user.userNikename;
			my.friendMobile=item.extras.userMobile;
			let data=[userFriend,my];
			let res = await this.$request.post('/user-admin/user/addFriend', {
				data: data
			});
			// #ifndef  H5
			let updateIsRead = "UPDATE 'message' SET isRead=1 WHERE \"id\"='{0}'".format(item.id);
			this.db.executeSQL(updateIsRead);
			item.isRead=1;
			// #endif
		}
	}
};
</script>

<style></style>
